package com.system.tree.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.system.role.dao.RoleRepository;
import com.system.role.entity.Role;
import com.system.tree.dao.TreeRepository;
import com.system.tree.entity.Tree;
import com.system.tree.entity.DTO.TreeChildrenDTO;
@Service
@Transactional
public class TreeService implements ITreeService {
	@Autowired	
	private TreeRepository treeRepository;
	@Autowired	
	private RoleRepository roleRepository;
	
	public void saveOrUpdate(Tree tree) {
		treeRepository.save(tree);
	}
	public void delete(Tree employee) {
		treeRepository.delete(employee);
	}
	public void delete(List<Long> ids) {
		List<Tree> employee =  (List<Tree>) treeRepository.findAll(ids);
		if(employee!=null) {
			treeRepository.delete(employee);
		}
	}
	@Transactional(readOnly = true)
	public Tree findOne(Long treeId) {
		return treeRepository.findOne(treeId);
	}
	@Transactional(readOnly = true)
	public TreeChildrenDTO findDTOAll(){
		List<Tree> children = (List<Tree>) treeRepository.findAll();
		return new TreeChildrenDTO(children);
	}
	public List<Tree> findAll(){
		return (List<Tree>) treeRepository.findAll();
	}
	//动态条件查询
	@Transactional(readOnly = true)
	public Page<Tree> findAll(Specification<Tree> spec, Pageable pageable){
		return treeRepository.findAll(spec, pageable);
	}
	@Override
	public List<Tree> getFather(Long id){
		return treeRepository.findTreeByFatherId(id);
	}
	@Override
	public List<Tree> getChildren(Long treeId) {
		Tree tree = treeRepository.findOne(treeId);
 		return tree.getChildren();
	}


	
}
